package com.shu.mmall.controller.backend;

import com.shu.mmall.common.Const;
import com.shu.mmall.common.ResponseCode;
import com.shu.mmall.common.Result;
import com.shu.mmall.pojo.Product;
import com.shu.mmall.pojo.User;
import com.shu.mmall.service.IProductService;
import com.shu.mmall.service.IUserService;
import com.shu.mmall.util.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author shujinhao
 */
@Controller
@RequestMapping("/product/manager")
public class ProductManagerController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IProductService productService;
    @RequestMapping(value = "productSave",method = RequestMethod.POST)
    @ResponseBody
    public Result productSave(HttpSession session, Product product){
        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if (EmptyUtil.isNull(user)){
            return Result.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        if (userService.checkAdminRole(user).isSuccess()){
            //增加产品的业务逻辑
            return productService.saveOrUpdateProduct(product);
        }else {
            return Result.createByErrorMessage("无权限操作");
        }


    }



}
